package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyLikeMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.LikeQueryObject;
import cn.wolfcode.trip.base.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.base.query.UserProfilesQueryObject;
import cn.wolfcode.trip.base.query.UserQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentMapper commentMapper;
    @Autowired
    private TagMapper tagMapper;

    public void save(StrategyComment strategyComment, String[] tags) {
        //设置评论者
        strategyComment.setUser(UserContext.getUser());
        //创建发布时间
        strategyComment.setCreateTime(new Date());
        // 保存评论
        commentMapper.insert(strategyComment);
        //保存该攻略地区的标签
        if (tags!=null&&tags.length > 0) {
            for (String tagString : tags) {
                Tag tag = new Tag();
                tag.setName(tagString);
                tagMapper.insert(tag);
                tagMapper.insertCommentTagRelation(strategyComment.getId(), tag.getId());
            }
        }
    }

    public PageInfo<StrategyComment> queryCommentsByStrategyId(StrategyCatalogQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyComment> list = commentMapper.queryCommentsByStrategyId(qo);
        return new PageInfo(list);
    }

    public StrategyComment getCommentById(Long commentId) {
        StrategyComment comment = commentMapper.selectByPrimaryKey(commentId);
        return comment;

    }

    public void saveReplyComment(StrategyComment strategyComment) {
        //设置评论者
        strategyComment.setUser(UserContext.getUser());
        //创建发布时间
        strategyComment.setCreateTime(new Date());
        // 保存评论
        strategyComment.setType(1);
        commentMapper.insertReplyCommrnt(strategyComment);
    }

    public StrategyComment getReplyCommentById(Long commentId) {
        return commentMapper.selectReplyByPrimaryKey(commentId);
    }

    public PageInfo queryforStrategyComments(UserProfilesQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyComment> strategyComments = commentMapper.selectStrategyCommentsByUserId(qo);
        return new PageInfo(strategyComments);
    }

    public PageInfo<StrategyComment> selectListForCommentUser(LikeQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyComment> strategyComments = commentMapper.selectReplyCommentsByUserId(qo);
        return new PageInfo(strategyComments);
    }

}
